package data

import (
	"context"
	"dataservice/internal/types"
	"encoding/json"
	"gitee.com/jmdvirus/jkgo/jk/jklog"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"io/ioutil"
)

type ZyDBInfo struct {
	client *mongo.Client
}

type zyAuthInfo struct {
	Username string `json:"dbzyuser"`
	Password string `json:"dbzypass"`
	Path     string `json:"dbzypath"`
}

var zyDB *ZyDBInfo
var db_name = "zy"

func zyConnectDB(path string, auth zyAuthInfo) *ZyDBInfo {
	cmd := ""
	jklog.L().Infoln("username: ", auth.Username)
	if len(auth.Username) == 0 {
		cmd = "mongodb://" + path
	} else {
		cmd = "mongodb://" + auth.Username + ":" + auth.Password + "@" + path
	}

	dbi := ZyDBInfo{}
	client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(cmd))
	if err != nil {
		jklog.L().Errorln(err)
	}
	dbi.client = client

	return &dbi
}

func init() {
	d, _ := ioutil.ReadFile("etc/dataservice.json")
	a := zyAuthInfo{}
	err := json.Unmarshal(d, &a)
	if err != nil {
		jklog.L().Errorln("error read json file ", err)
	}
	zyDB = zyConnectDB(a.Path, a)
}

func GetZhongChengYaoList(start, limit int, query types.ZhongChengYaoQuery) *types.ZhongChengYaoList {
	var sclist types.ZhongChengYaoList

	coll := zyDB.client.Database(db_name).Collection("zcy")
	filter := bson.M{}
	if len(query.LeiXing) > 0 {
		filter["leixing"] = query.LeiXing
	}
	cur, err := coll.Find(context.TODO(), filter)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	err = cur.All(context.TODO(), &sclist.Item)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	max := len(sclist.Item)
	jklog.L().Infoln("start ", start, ", limit ", limit, "max: ", max)
	if start+limit > max {
		jklog.L().Errorln("max count ", max)
		return &sclist
	}
	startt := int64(start)
	limitt := int64(limit)
	findOp := options.FindOptions{Skip: &startt, Limit: &limitt}
	cur, _ = coll.Find(context.TODO(), filter, &findOp)
	err = cur.All(context.TODO(), &sclist.Item)
	if err != nil {
		jklog.L().Errorln("error: ", err)
	}
	jklog.L().Infoln("len: ", len(sclist.Item))
	return &sclist
}

func GetZhongChengYaoNames(query types.ZhongChengYaoNamesQuery) *types.ZhongChengYaoNames {
	var sclist types.ZhongChengYaoNames

	coll := zyDB.client.Database(db_name).Collection("names")
	filter := bson.M{}
	cur, err := coll.Find(context.TODO(), filter)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	err = cur.All(context.TODO(), &sclist.Names)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	jklog.L().Infoln("len: ", len(sclist.Names))
	return &sclist
}

func GetZhongChengYaoZhongYaoCai(query types.ZhongChengYaoZhongYaoQuery) *types.ZhongChengYaoZhongYaoList {
	var sclist types.ZhongChengYaoZhongYaoList

	coll := zyDB.client.Database(db_name).Collection("zyc")
	filter := bson.M{"yaoming": query.Name}
	cur, err := coll.Find(context.TODO(), filter)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	err = cur.All(context.TODO(), &sclist.Items)
	if err != nil {
		jklog.L().Errorln(err)
		return &sclist
	}
	jklog.L().Infoln("len: ", len(sclist.Items))
	return &sclist
}
